﻿//使用对象来建立摄像头的各种控制
export const camera = function () {
    //属性
    var thiz = this;
    thiz.id;
    thiz.key;
    thiz.cam_divid;
    thiz.camip;
    thiz.camid;
    thiz.img;
    thiz.src;
    thiz.camType;
    thiz.saddr = "api.zhiyun360.com:8002";

    //云服务初始化
    thiz.initZCloud = function (id, key) {
        thiz.id = id;
        thiz.key = key;
    }
    //设置服务器地址
    thiz.setServerAddr = function (saddr) {
        thiz.saddr = saddr;
    }
    //摄像头初始化
    thiz.initCamera = function (gwip, cam) {
        thiz.camip = gwip;
		if(!gwip) {
			thiz.camip = "zxbeegw" + thiz.id + ".zhiyun360.com"
		}
        thiz.camid = cam;
    }

    //设置视频图像显示的标签id
    thiz.setDiv = function (divID) {
        thiz.cam_divid = divID;
    }


    thiz.__getZCameraAddr = function () {
        return [thiz.camip, thiz.camid];
    }

    //视频开
    thiz.openVideo = function () {
		thiz.src = "http://" + thiz.camip + "/stream/" + encodeURIComponent(thiz.camid)
        console.log(thiz.src);
		return thiz.src
    }
    //视频关闭
    thiz.closeVideo = function () {
        thiz.src = '/static/bg.jpg';
		return thiz.src
    }

    function ptz(x, y, z) {
		let url = "http://" + thiz.camip + "/ptz/relativemove/" + encodeURIComponent(thiz.camid)
		/* uni-app版 */
		uni.request({
			method: "POST",
			url,
			data: JSON.stringify({ x: x, y: y, z: z }),
			success: function (data, status) {
				if(data){
					console.log('success');
				}
			}
		});
    }

    //向上
    thiz.ptzUpSubmit = function () {
        ptz(0, 0.05, 0)
    }
    //向下
    thiz.ptzDownSubmit = function () {
        ptz(0, -0.05, 0)
    }
    //向左
    thiz.ptzLeftSubmit = function () {
        ptz(-0.05, 0, 0)
    }
    //向右
    thiz.ptzRightSubmit = function () {
        ptz(0.05, 0, 0)
    }

    //截屏
    thiz.snapshot = function () {
        // var ac = thiz.__getZCameraAddr()
        // var imgURL = "http://" + ac[0] + "/snapshot/" + encodeURIComponent(ac[1]);;
        // var oPop = window.open(imgURL, "mysnapshot", "width=640,height=480,top=100,left=400");
    }


}